class Solution:
    def countBattleships(self, board):
        if not board:
            return 0
        m, n = len(board), len(board[0])
        count = 0
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'X':
                    if i > 0 and board[i-1][j] == 'X':
                        continue
                    if j > 0 and board[i][j-1] == 'X':
                        continue
                    count += 1
        return count

if __name__ == '__main__':
    board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
    print(Solution().countBattleships(board))

# 1、遍历每个格子，如果遇到 'X'，检查上面和左边的格子是否也有 'X'，避免重复计数。
# 2、只有当前的 'X' 是战舰的起点（即没有被上面或左边的 'X' 覆盖时），才算一个新的战舰。
# 3、通过这种方式，能够准确地计算出战舰的数量。